import {Component} from '@angular/core';
import {InfiniteScroll, IonicPage, NavController} from 'ionic-angular';


import {Util} from "@providers/util";
import {DialogProvider} from "@providers/dialog";
import {PagingConfig, PagingProvider} from "@providers/paging";
import {StorageProvider} from "@providers/storage";
import {WorkerProvider} from "@providers/interface/worker";

@IonicPage()
@Component({
  selector: 'page-cost_list',
  templateUrl: 'cost_list.html',
})
export class CostListPage {
  datas: any =[];
  //后台将查询工资总数和分组写在同一接口，查询出来的工资总数有一条，分组工资确有很多条
  sumTotal: any[]=[];//总工资
  listGroup: any[] = [];//工资分组
  costItems:any=[];//工资明细
  userInfo: any;
  datasConfig: PagingConfig = new PagingConfig();
  time: any = '7';
  timeStarts:any;
  timeEnds:any;

  constructor(private dialog: DialogProvider,
              public navCtrl: NavController,
              private storageService: StorageProvider,
              private pagingService: PagingProvider,
              private worker:WorkerProvider) {
    this.userInfo = this.storageService.read("userInfo");
    this.timeStarts = Util.getMonth(0).toString()+'-01';
    this.timeEnds = Util.getDate(0);
  }
  push(page: string, data?: any) {
    this.navCtrl.push(page, data);
  }

  ionViewDidLoad() {
    this.dialog.showLoading();
    this.getData(data=>{
      this.datas = data;
      this.sumTotal = this.datas[1].HZ_JSON;
      this.listGroup = this.datas[0].FL_JSON;
      this.costItems = this.datas.data;
    },this.datasConfig);

  }

  /**
   * 获取数据
   * @param pagingConfig 分页
   * @param fn 方法
   */
  private getData(fn: (value) => void, pagingConfig) {

    let _next = (data) => {
      this.pagingService.get({
        interfaceId: "csm.som.cssOrderquery.selectSalaryGroup",
        // data: {"WORKER": this.userInfo["PARTYID"],"FWLX":"2", 'time': this.time}
        data: {"LXDH": this.userInfo.MOBILE, "RQ_S": this.timeStarts, "RQ_Z": this.timeEnds,}
      }, pagingConfig).toPromise().then(data1 => {
        this.dialog.hideLoading();
        Object.assign(data, data1);
        fn(data);
      }).catch((error) => {
        this.dialog.hideLoading();
        this.dialog.showAlert("请检查网络" + error);
      });
    };

    this.worker.getSfCost(
      {"PARTYID": this.userInfo.EXTID, "SJQ": this.timeStarts, "SJZ": this.timeEnds}
    ).then(data => {
      _next(data);
    }).catch((error) => {
      this.dialog.hideLoading();
      this.dialog.showAlert("请检查网络" + error);
    });
  }


  /**
   * 下拉刷新
   * @param infiniteScroll
   */
  getMoreDatas(infiniteScroll: InfiniteScroll) {
    this.dialog.showLoading();
    this.getData((value) => {
      this.datas = value;
      this.sumTotal = this.datas[1].HZ_JSON;
      this.listGroup = this.datas[0].FL_JSON;
      this.costItems = this.datas.data;
      infiniteScroll.complete();
    }, this.datasConfig);
  }


}
